package Test;

import java.util.concurrent.TimeUnit;

public class TestDemo9 {
    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (a) {
                    System.out.println("获取到a锁，尝试获取b锁");
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (b) {
                        System.out.println("获取到b锁");
                    }
                }
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (b) {
                    System.out.println("获取到b锁，尝试获取a锁");
                    synchronized (a) {
                        System.out.println("获取到a锁");
                    }
                }
            }
        }).start();
    }
}
